package view;
import java.awt.event.ActionEvent;
import java.util.Collection;

import javax.swing.AbstractAction;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import javax.swing.WindowConstants;

import model.dtos.IngredienteDTO;
import model.dtos.MesaDTO;
import model.persistence.daos.IngredienteDAO;

import org.jdesktop.application.Action;
import org.jdesktop.application.Application;
import org.jdesktop.application.ApplicationActionMap;

import controllers.AdministracionController;

import javax.swing.SwingUtilities;



/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class IngredienteBusqueda extends javax.swing.JPanel {
	private JTextField jTextFieldIngredienteID;
	private JButton jButtonIngredienteAceptar;
	private JLabel jLabelngred;
	private JTextField jTextFieldDesc;
	private JLabel jLabel3;
	private JTextField jTextFieldNombre;
	private JLabel jLabel2;
	private JLabel jLabel1;
	private JButton jButtonBuscar;
	private AbstractAction abstractActionIngredienteAceptar;
	private JButton jButtonIngredienteCancelar;
	private static IngredienteBusqueda frameIngredienteBusquedaInstancia;

	
	public static IngredienteBusqueda getInstancia(){
		if(frameIngredienteBusquedaInstancia==null){
			frameIngredienteBusquedaInstancia=new IngredienteBusqueda();
		}
		return frameIngredienteBusquedaInstancia;
	}
	
	public IngredienteBusqueda() {
		super();
		initGUI();
	}
	
	private void initGUI() {
		try {
			this.setLayout(null);
			{
				jTextFieldIngredienteID = new JTextField();
				this.add(jTextFieldIngredienteID);
				jTextFieldIngredienteID.setBounds(203, 38, 63, 23);
			}
			{
				jButtonIngredienteAceptar = new JButton();
				this.add(jButtonIngredienteAceptar);
				jButtonIngredienteAceptar.setText("Aceptar");
				jButtonIngredienteAceptar.setBounds(68, 140, 134, 23);
				jButtonIngredienteAceptar.setAction(getAbstractActionIngredienteAceptar());
			}
			{
				jButtonIngredienteCancelar = new JButton();
				this.add(jButtonIngredienteCancelar);
				this.add(getJLabelngred());
				this.add(getJButtonBuscar());
				this.add(getJLabel1());
				this.add(getJLabel2());
				this.add(getJTextFieldNombre());
				this.add(getJLabel3());
				this.add(getJTextFieldDesc());
				jButtonIngredienteCancelar.setText("Cancelar");
				jButtonIngredienteCancelar.setBounds(213, 139, 119, 25);
				jButtonIngredienteCancelar.setAction(getAppActionMap().get("actionCancelar"));
			}
			this.setSize(400, 250);
			this.setPreferredSize(new java.awt.Dimension(405, 174));
			Application.getInstance().getContext().getResourceMap(getClass()).injectComponents(this);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private AbstractAction getAbstractActionIngredienteAceptar() {
		if(abstractActionIngredienteAceptar == null) {
			abstractActionIngredienteAceptar = new AbstractAction("Aceptar", null) {
				public void actionPerformed(ActionEvent evt) {
					
					if(!jTextFieldDesc.getText().equals("") && !jTextFieldNombre.getText().equals(""))
					{
						IngredienteDTO ingrediente=new IngredienteDTO();
						ingrediente.setDescripcion(jTextFieldDesc.getText());
						ingrediente.setId(Integer.valueOf(jTextFieldIngredienteID.getText()));
						ingrediente.setNombre(jTextFieldNombre.getText());
						AdministracionController.getInstancia().guardarIngrediente(ingrediente);
						jTextFieldDesc.setText("");
						jTextFieldIngredienteID.setText("");
						jTextFieldNombre.setText("");
						JOptionPane.showMessageDialog(null,"Ingrediente modificado ","Info", JOptionPane.INFORMATION_MESSAGE);
					}
					else
					{
						JOptionPane.showMessageDialog(null," Error","Error", JOptionPane.INFORMATION_MESSAGE);
					}
					
					
				}
			};
		}
		return abstractActionIngredienteAceptar;
	}
	
	@Action
	public void actionCancelar() {
		frameIngredienteBusquedaInstancia.setVisible(false);
		jTextFieldNombre.setEditable(false);
		jTextFieldDesc.setEditable(false);
		jTextFieldDesc.setText("");
		jTextFieldNombre.setText("");
		jTextFieldIngredienteID.setText("");
	}
	
    /**
    * Returns the action map used by this application.
     * Actions defined using the Action annotation
     * are returned by this method
     */
	private ApplicationActionMap getAppActionMap() {
		return Application.getInstance().getContext().getActionMap(this);
	}
    
    private JLabel getJLabelngred() {
    	if(jLabelngred == null) {
    		jLabelngred = new JLabel();
    		jLabelngred.setBounds(71, 42, 159, 14);
    		jLabelngred.setText("ID del ingrediente:");
    	}
    	return jLabelngred;
    }
    
    private JButton getJButtonBuscar() {
    	if(jButtonBuscar == null) {
    		jButtonBuscar = new JButton();
    		jButtonBuscar.setBounds(278, 38, 87, 23);
    		jButtonBuscar.setName("jButtonBuscar");
    		jButtonBuscar.setAction(getAppActionMap().get("Buscar"));
    	}
    	return jButtonBuscar;
    }
    
	@Action
	public void Buscar() {
		
		Collection<IngredienteDTO> _ingredientes=AdministracionController.getInstancia().getIngredientes();
		for (IngredienteDTO _ingrediente : _ingredientes)
		{
			if (_ingrediente.getId() == Integer.valueOf(jTextFieldIngredienteID.getText()))
			{
				//Mostrar datos de mesa en pantalla
				jTextFieldNombre.setText(_ingrediente.getNombre());
				jTextFieldDesc.setText(_ingrediente.getDescripcion());
				jTextFieldNombre.setEditable(true);
				jTextFieldDesc.setEditable(true);
			}
		}
	}
	
	private JLabel getJLabel1() {
		if(jLabel1 == null) {
			jLabel1 = new JLabel();
			jLabel1.setBounds(80, 10, 298, 16);
			jLabel1.setText("MODIFICACION DE INGREDIENTE");
		}
		return jLabel1;
	}
	
	private JLabel getJLabel2() {
		if(jLabel2 == null) {
			jLabel2 = new JLabel();
			jLabel2.setBounds(68, 68, 116, 16);
			jLabel2.setText("Nombre");
		}
		return jLabel2;
	}
	
	private JTextField getJTextFieldNombre() {
		if(jTextFieldNombre == null) {
			jTextFieldNombre = new JTextField();
			jTextFieldNombre.setBounds(202, 65, 154, 23);
			jTextFieldNombre.setEditable(false);
		}
		return jTextFieldNombre;
	}
	
	private JLabel getJLabel3() {
		if(jLabel3 == null) {
			jLabel3 = new JLabel();
			jLabel3.setBounds(68, 96, 116, 16);
			jLabel3.setText("Descripcion");
		}
		return jLabel3;
	}
	
	private JTextField getJTextFieldDesc() {
		if(jTextFieldDesc == null) {
			jTextFieldDesc = new JTextField();
			jTextFieldDesc.setBounds(202, 93, 154, 23);
			jTextFieldDesc.setEditable(false);
		}
		return jTextFieldDesc;
	}

}
